package com.newshub.admin.controller;

import com.newshub.common.Result;
import com.newshub.business.dto.PageResult;
import com.newshub.admin.dto.RssFeedQueryDTO;
import com.newshub.admin.entity.RssFeed;
import com.newshub.admin.service.RssFeedService;
import com.newshub.logging.annotation.BusinessLog;
import com.newshub.logging.annotation.LogLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/rss-feeds")
@CrossOrigin(origins = "*")
public class AdminRssFeedController {

    @Autowired
    private RssFeedService rssFeedService;

    @PostMapping("/page")
    @BusinessLog(module = "RSS源管理", description = "分页查询RSS源列表", level = LogLevel.INFO)
    public Result<PageResult<RssFeed>> page(@RequestBody RssFeedQueryDTO queryDTO) {
        try {
            PageResult<RssFeed> page = rssFeedService.page(queryDTO);
            return Result.success(page);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @BusinessLog(module = "RSS源管理", description = "查询RSS源详情", level = LogLevel.INFO)
    public Result<RssFeed> detail(@PathVariable Integer id) {
        try {
            RssFeed feed = rssFeedService.getById(id);
            if (feed == null) return Result.error(404, "记录不存在");
            return Result.success(feed);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    @PostMapping
    @BusinessLog(module = "RSS源管理", description = "创建RSS源", level = LogLevel.INFO)
    public Result<String> create(@RequestBody RssFeed feed) {
        try {
            boolean ok = rssFeedService.create(feed);
            return ok ? Result.success("创建成功") : Result.error("创建失败");
        } catch (Exception e) {
            return Result.error("创建失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    @BusinessLog(module = "RSS源管理", description = "更新RSS源", level = LogLevel.INFO)
    public Result<String> update(@PathVariable Integer id, @RequestBody RssFeed feed) {
        try {
            feed.setId(id);
            boolean ok = rssFeedService.update(feed);
            return ok ? Result.success("更新成功") : Result.error("更新失败");
        } catch (Exception e) {
            return Result.error("更新失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @BusinessLog(module = "RSS源管理", description = "删除RSS源", level = LogLevel.WARN)
    public Result<String> delete(@PathVariable Integer id) {
        try {
            boolean ok = rssFeedService.softDelete(id);
            return ok ? Result.success("删除成功") : Result.error("删除失败");
        } catch (Exception e) {
            return Result.error("删除失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}/status")
    @BusinessLog(module = "RSS源管理", description = "更新RSS源状态", level = LogLevel.INFO)
    public Result<String> updateStatus(@PathVariable Integer id, @RequestParam Integer status) {
        try {
            boolean ok = rssFeedService.updateStatus(id, status);
            return ok ? Result.success("状态更新成功") : Result.error("状态更新失败");
        } catch (Exception e) {
            return Result.error("状态更新失败: " + e.getMessage());
        }
    }

    @GetMapping("/enabled")
    @BusinessLog(module = "RSS源管理", description = "查询启用的RSS源列表", level = LogLevel.INFO)
    public Result<List<RssFeed>> listEnabled() {
        try {
            return Result.success(rssFeedService.listEnabled());
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
}




